﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.EditorExt;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.GlobeCore;
using ESRI.ArcGIS.NetworkAnalysis;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;

namespace DIWAGIS.ApplicationLayer.XemThongTin
{
    public partial class frmCapNhatThongTinToaDo : Form
    {
        private IMapControl4 m_mapControl;
        public IMapControl4 mapControl
        {
            get { return m_mapControl; }
            set { m_mapControl = value; }
        }

        private ILayer m_SelectedLayer;
        public ILayer SelectedLayer
        {
            get { return m_SelectedLayer; }
            set { m_SelectedLayer = value; }
        }

        private IFeature m_SelectedFeature;
        private IEngineEditor m_editor;
        private ILayer targetLayer;

        public IFeature SelectedFeature
        {
            get { return m_SelectedFeature; }
            set
            {
                m_SelectedFeature = value;
                LoadData();
                if (m_editor.EditState == esriEngineEditState.esriEngineStateEditing)
                {
                    IEngineEditTask edittask = m_editor.GetTaskByUniqueName("ControlToolsEditing_ModifyFeatureTask");
                    if (edittask != null)
                    {
                        m_editor.CurrentTask = edittask;
                        //m_toolEdit.CurrentTool = m_toolEdit.GetItem(2).Command as ITool;
                    }
                }
            }
        }

        public IEngineEditor editor
        {
            get { return m_editor; }
            set { m_editor = value; }
        }

        public frmCapNhatThongTinToaDo()
        {
            InitializeComponent();
        }

        private void frmCapNhatThongTinToaDo_Load(object sender, EventArgs e)
        {
            try
            {
                //IList<ILayer> lstLayer = EAGL.CommonTools.WorkingWithLayer.GetAllLayer(m_mapControl);
                //for (int i = 0; i < lstLayer.Count; i++)
                //{
                //    cboLayer.Properties.Items.Add(lstLayer[i].Name);
                //}
                //if (lstLayer.Count > 0)
                //    cboLayer.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                Class.clsStaticFunctions.ThongBaoLoi(ex);
            }
        }
       
        private void btnCloseForm_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                Class.clsStaticFunctions.ThongBaoLoi(ex);
            }
        }

        private DataTable tblData;
        DataRow newRow;
        private void CreateTable()
        {
            DataColumn colToaDoX = new DataColumn("colToaDoX", typeof(double));
            DataColumn colToaDoY = new DataColumn("colToaDoY", typeof(double));

            tblData = new DataTable();
            tblData.Columns.Add(colToaDoX);
            tblData.Columns.Add(colToaDoY);
        }

        public void ClearData()
        {
            if (tblData != null)
            {
                tblData.Rows.Clear();
            }
        }

        public void LoadData()
        {
            if (m_SelectedFeature != null)
            {
                CreateTable();

                if (((IFeatureLayer)targetLayer).FeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                {
                    IPoint getPoint = m_SelectedFeature.ShapeCopy as IPoint;
                    newRow = tblData.NewRow();
                    newRow[0] = getPoint.X;
                    newRow[1] = getPoint.Y;
                    tblData.Rows.Add(newRow);
                }
                else
                {
                    IPointCollection pointCollection = m_SelectedFeature.ShapeCopy as IPointCollection;
                    IPoint getPoint;
                    for (int i = 0; i < pointCollection.PointCount; i++)
                    {
                        getPoint = pointCollection.get_Point(i);
                        newRow = tblData.NewRow();
                        newRow[0] = getPoint.X;
                        newRow[1] = getPoint.Y;
                        tblData.Rows.Add(newRow);
                    }
                }
                gridControl.DataSource = tblData;
                //if(((IFeatureLayer)targetLayer).FeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                //{

                //}
                //else if (((IFeatureLayer)targetLayer).FeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
                //    gridControl.DataSource = tblData;
            }
        }

        private void gridView_CustomDrawRowIndicator(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                //Class.clsStaticFunctions.ShowIndicator(e);
            }
            catch (Exception ex)
            {
                Class.clsStaticFunctions.ThongBaoLoi(ex);
            }
        }

        private void gridView_RowClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataRowView currentDataRowView = (DataRowView)gridControl.CurrentRow.DataBoundItem;
                DataRow selectedRow = currentDataRowView.Row;
                IPoint point = new PointClass();
                if (selectedRow != null)
                {
                    point.X = (double)selectedRow[0];
                    point.Y = (double)selectedRow[1];
                    IRgbColor clr = new RgbColor();
                    clr.RGB = Color.Red.ToArgb();
                    Class.clsTuongTacBanDo objTuongTacBanDo = new Class.clsTuongTacBanDo();
                    objTuongTacBanDo.FlashGeometry(m_mapControl, point, clr, m_mapControl.ActiveView.ScreenDisplay, 2, 400);
                }
            }
            catch (Exception ex)
            {
                Class.clsStaticFunctions.ThongBaoLoi(ex);
            }
        }

        private void btnChonDoiTuong_Click(object sender, EventArgs e)
        {
            try
            {
                IEngineEditLayers layerEdit = m_editor as IEngineEditLayers;
                targetLayer = layerEdit.TargetLayer;
                //targetLayer = EAGL.CommonTools.WorkingWithLayer.GetLayerByName(cboLayer.Text, mapControl);

                DIWAGIS.ApplicationLayer.BoCongCuQuanLy.ToolUpdateCoordinate ToolUpdateCoordinate = new DIWAGIS.ApplicationLayer.BoCongCuQuanLy.ToolUpdateCoordinate();
                ToolUpdateCoordinate.frmCapNhatToaDo = this;
                ToolUpdateCoordinate.targetLayer = targetLayer;
                ToolUpdateCoordinate.OnCreate(m_mapControl);
                ToolUpdateCoordinate.editor = m_editor;
                m_mapControl.CurrentTool = ToolUpdateCoordinate;
            }
            catch (Exception ex)
            {
                Class.clsStaticFunctions.ThongBaoLoi(ex);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                DataRowView currentDataRowView = (DataRowView)gridControl.CurrentRow.DataBoundItem;
                DataRow selectedRow = currentDataRowView.Row;
                IPoint point = new PointClass();
                if (selectedRow != null)
                {
                    point.X = (double)selectedRow[0];
                    point.Y = (double)selectedRow[1];
                    if (((IFeatureLayer)targetLayer).FeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                    {
                        SelectedFeature.Shape = point as IGeometry;
                        SelectedFeature.Store();
                    }
                    else
                    {
                        IPointCollection pointCollection = m_SelectedFeature.ShapeCopy as IPointCollection;
                        IPoint getPoint = new PointClass();
                        for (int i = 0; i < pointCollection.PointCount; i++)
                        {
                            getPoint.X = double.Parse(tblData.Rows[i][0].ToString());
                            getPoint.Y = double.Parse(tblData.Rows[i][1].ToString());
                            pointCollection.UpdatePoint(i, getPoint);
                        }
                        SelectedFeature.Shape = pointCollection as IGeometry;
                        SelectedFeature.Store();
                    }
                    m_mapControl.ActiveView.Refresh();
                    if (m_editor.EditState == esriEngineEditState.esriEngineStateEditing)
                    {
                        IEngineEditTask edittask = m_editor.GetTaskByUniqueName("ControlToolsEditing_ModifyFeatureTask");
                        if (edittask != null)
                        {
                            m_editor.CurrentTask = edittask;
                            //m_toolEdit.CurrentTool = m_toolEdit.GetItem(2).Command as ITool;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Class.clsStaticFunctions.ThongBaoLoi(ex);
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            try
            {
                m_mapControl.CurrentTool = null;
                this.Close();
            }
            catch (Exception ex)
            {
                Class.clsStaticFunctions.ThongBaoLoi(ex);
            }
        }

        private void frmCapNhatThongTinToaDo_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                m_mapControl.CurrentTool = null;
            }
            catch (Exception ex)
            {
                Class.clsStaticFunctions.ThongBaoLoi(ex);
            }
        }
    }
}
